// gjw/pages/doubao/buy.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
		var url = wx.getStorageSync('siteUrl')
    that.setData({
      siteUrl: url,
      info: options,
      sum_price: Number(options.price),
      all_money: Number(options.price) + Number(options.exfee),
      yunfei: Number(options.exfee)
    })
    that.get_list()
  },

  // 判断有没有地址
  get_list: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Addresslist',
      data: {
        openid: openid
      },
      success: function (res) {
        
        if (res.data.code == 200) {
          var arry = res.data.res
          if (arry.length == 1) {
            that.setData({
              comdata: arry[0]
            })
          } else {
            for (var i = 0; i < arry.length; i++) {
              if (arry[i].state == 1) {
                that.setData({
                  comdata: arry[i]
                })
              }
            }
          }
        } else if (res.data.code == 500) {
          wx.showModal({
            title: '提示',
            content: '您暂时还没有收货地址，立刻去新建收货地址吧~',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../order/add_area',
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }

      }
    })

  },


  sub: function (e) {
    var that = this
    var formid = e.detail.formId
    var openid = wx.getStorageSync('openid')
    var db_id = that.data.info.id
    var price = that.data.info.price
    var phone = that.data.comdata.phone
    var addressname = that.data.comdata.addressname
    var address = that.data.comdata.address + that.data.comdata.addressdetail
    // var num = that.data.num
    var sum = that.data.all_money
    app.util.request({
      url: 'entry/wxapp/Marketorder',
      data: {
        openid: openid, formid: formid, p_id: db_id, type: 3, ofee: price, phone: phone, address: address, num: 1, ofees: sum,
        name: addressname
      },
      success: function (res) {
        
        that.setData({
          res_id: res.data.res_id
        })
        var res_id = that.data.res_id
        if (res.data.code == 200) {
          app.util.request({
            url: 'entry/wxapp/Marketorderpay',
            data: {
              openid: openid, res_id: res_id, ofees: sum
            },
            success: function (res) {
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  app.util.request({
                    url: 'entry/wxapp/Marketorderpaysuccess',
                    data: {
                      res_id: res_id, id: db_id,
                    },
                    success: function (res) {
                      wx.showModal({
                        title: '提示',
                        content: '您的订单已经付款成功，可以到订单页中查看',
                        showCancel: true,
                        cancelText: '继续逛逛',
                        cancelColor: '',
                        confirmText: '我的订单',
                        confirmColor: '',
                        success: function (res) {
                          if (res.confirm) {
                            wx.navigateTo({
                              url: '../order/index',
                            })
                          } else if (res.cancel) {
                            wx.reLaunch({
                              url: '../index/index',
                            })
                          }
                        },
                        fail: function (res) { },
                        complete: function (res) { },
                      })
                      wx.reLaunch({
                        url: '../index/index',
                      })
                    }
                  })

                },
                'fail': function (res) {
                  ;
                  wx.showToast({
                    title: '支付失败',
                    icon: "loading",
                  })
                },
                'complete': function (res) {
                  ;
                  wx.showToast({
                    title: '取消支付',
                    icon: "loading",
                  })
                }
              })

            }

          })

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var areaInfo = wx.getStorageSync('areaInfo')
    that.setData({
      comdata: areaInfo
    })

    that.get_list()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})